package com.jn.jetpack.presenter;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.jn.jetpack.contract.QAndAContract;
import com.jn.jetpack.dao.QAndADao;
import com.jn.jetpack.entity.QAndAEntity;
import com.jn.jetpack.service.QAndAService;
import com.jn.jetpack.utils.LogUtils;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class QAndAPresenter implements QAndAContract.IQAndAPresenter {
    private Context mContext;
    private QAndAContract.IQAndAView mQAndAView;
    private final CompositeDisposable mCompositeDisposable;

    public QAndAPresenter(QAndAContract.IQAndAView qAndAView, Context context) {
        mQAndAView = qAndAView;
        mContext = context;
        mCompositeDisposable = new CompositeDisposable();
    }

    @Override
    public void getData() {//get方式异步请求
        if (mQAndAView != null) {
            mQAndAView.onHttpLoading(true);
        }
        String url = "https://shanhe.kim/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        QAndAService service = retrofit.create(QAndAService.class);
        service.getData("json")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<QAndAEntity>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull QAndAEntity qAndAEntity) {
                        if (mQAndAView != null) {
                            mQAndAView.onHttpLoading(false);
                            mQAndAView.showData(qAndAEntity);
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        if (mQAndAView != null) {
                            mQAndAView.onHttpLoading(false);
                            mQAndAView.onHttpFail(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onCreate(@androidx.annotation.NonNull LifecycleOwner owner) {
        LogUtils.d("asdf", "====onCreate===== ");
        getData();
    }

    @Override
    public void onDestroy(@androidx.annotation.NonNull LifecycleOwner owner) {
        LogUtils.d("asdf", "====onDestroy===== ");
        mCompositeDisposable.dispose();
    }
}